package com.os.sys.controller;

import com.os.sys.config.MyProps;
import com.os.sys.pojo.RoleType;
import com.os.sys.service.RoleTypeService;
import com.os.util.Tools;
import entity.Param;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/roletype")
@Slf4j
//这是一个典型的在列表上直接增加，修改，查询的示例
public class RoleTypeController {
    @Autowired
    private RoleTypeService roleTypeService;
    @Autowired
    private MyProps myProps;

    @RequestMapping(value="/selectByPage", method=RequestMethod.GET)
    public Result doSel(@RequestParam(value="page",required = false) Integer page,
                               @RequestParam(value="pageSize",required = false) Integer pageSize,
                               @RequestParam(value="roletype_name",required = false) String roletype_name){
        Map map = new HashMap();
        List paramList = new ArrayList();
        if(roletype_name!=null && roletype_name.length()>0) {
            paramList.add(new Param("roletype_name", roletype_name + ""));
        }
        map.put("condstr", Tools.genCondStr("", paramList));
        //服务端分页
        String limit = " limit " + ((page - 1) * pageSize) + "," + pageSize + " ";
        map.put("limit", limit);
        List list = roleTypeService.selectByPage(map);
        Result result = new Result(true, StatusCode.OK,"查询成功",list);
        result.setTotalCount(new Long(list.size()));
        List resultList = new ArrayList();
        resultList.add(result);
        result.setTotal(list.size());
        return result;
    }

    @ApiOperation(value = "按照ID查询信息" , notes = "按照ID查询信息")
    @ApiImplicitParam(name = "roletype_id",value = "用户ID",required = true,dataType = "int")
    @RequestMapping(value="/{roletype_id}", method=RequestMethod.GET)
    public Result selectById(@PathVariable int roletype_id){
        return new Result(true, StatusCode.OK,"查询成功", roleTypeService.selectById(roletype_id));
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result insert(@RequestBody List<RoleType> roleTypeList){
        RoleType roleType = roleTypeList.get(0);
        roleTypeService.insert(roleType);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Result update(@RequestBody List<RoleType> roleTypeList){
        RoleType roleType = roleTypeList.get(0);
        //roleType.setRoletype_id(roletype_id);
        log.debug("roletype_id = "+roleType.getRoletype_id());
        roleTypeService.update(roleType);
        return new Result(true, StatusCode.OK,"修改成功", roleType);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public Result delete(@RequestBody List<RoleType> roleTypeList){
        RoleType roleType = roleTypeList.get(0);
        roleTypeService.deleteById(roleType.getRoletype_id());
        return new Result(true, StatusCode.OK,"删除成功");
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "index";
    }

}
